/**
 * 扩展的http请求
 * 新增请求page、form、noPermission页面
 */
layui.define(['toast'], function (exports) {

    let toast = layui.toast

    // 请求页面节流阀（防抖即短时间内点击次数）
    let throttle = false

    const render_html = function (res, dialog) {
        let form = layui.form

        if (!dialog || typeof dialog !== 'object') {
            dialog = {}
        }

        let title = dialog['title'] || 'Form'
        let area = dialog['area'] || 'middle'
        let offset = 'auto'
        let width = $(window).width()
        let height = $(window).height()

        if (area === 'large') {
            width = width - 40
            height = height - 40
        } else if (area === 'middle') {

            offset = '80px'
            // 手机端
            if (width > 650) {
                width = width * 0.6
                height = height * 0.8
            } else {
                width = width - 20
                height = height * 0.8
            }

        } else if (area === 'small') {

            offset = '80px'
            // 手机端
            if (width > 650) {
                width = width * 0.4
                height = height * 0.4
            } else {
                width = width - 20
                height = height * 0.8
            }

        } else if (Array.isArray(area)) {
            width = area[0]
            height = area[1]
        } else {
            width = width - 40
            height = height - 40
        }

        if (width !== 'auto') {
            width = width + 'px'
        }

        if (height !== 'auto') {
            height = height + 'px'
        }

        layer.open({
            title: title,
            type: 1,
            offset: offset,
            area: [width, height],
            btnAlign: 'c',
            closeBtn: 1,
            content: res,
            scrollbar: false,
            success: function (layero, index) {
                form.render()
            },
        })
    }

    const request = (url, method, data, dialog) => {

        if (throttle) {
            return false
        }
        throttle = true

        return $.ajax({
            url: url,
            data: data,
            type: method,
        }).then((res, text_status, xhr) => {

            const content_type = xhr.getResponseHeader('Content-Type');

            if (content_type.indexOf('application/json') !== -1) {
                // JSON 数据
                if (dialog) {
                    toast.response(res);
                }
            } else if (content_type.indexOf('text/html') !== -1) {
                // HTML 数据
                if (dialog) {
                    render_html(res, dialog);
                }
            } else {
                // 纯文本或其他类型
                console.log('Text data:', data);
            }
            return res;
        }).catch(error => {

            const content_type = error.getResponseHeader('Content-Type');

            if (content_type.indexOf('application/json') !== -1) {
                // JSON 数据
                toast.response(error.responseText);
            } else if (content_type.indexOf('text/html') !== -1) {
                // HTML 数据
                render_html(error.responseText, dialog);
            } else {
                // 纯文本或其他类型
                console.log('Text data:', data);
            }

            return error;
        }).always(res => {
            throttle = false;
        })
    }

    /**
     *
     * @param url
     * @param data
     * @param dialog
     */
    const api = {
        get: (url, data, dialog) => {
            return request(url, 'get', data, dialog);
        },
        post: (url, data, dialog) => {
            return request(url, 'post', data, dialog);
        }
    }
    
    exports('api', api)
})